<?php

/**
 * 文章首页
 */
class ArticleAction extends UserAction {

    public $dao;

    public function _initialize() {
        parent::_initialize();
        $this->model = D("Article");
        $_POST['content'] = $_POST['info']['content'];
    }

    function _filter(&$map) {
        $map['uid'] = session('uid');
    }

    public function add() {
        if (IS_POST) {
            if (!$_POST['title'] || !$_POST['info']['content']) {
                $this->error('请填写完整信息');
            }

            //校验敏感词
            $checkTitle = D('Badword')->checkBadword($_POST['title']);
            if ($checkTitle['status'] == false)
                $this->error('标题包含敏感词 -- ' . $checkTitle['data']);

            //校验敏感词
            $checkTitle = D('Badword')->checkBadword($_POST['info']['content']);
            if ($checkTitle['status'] == false)
                $this->error('内容包含敏感词 -- ' . $checkTitle['data']);

            $data = array(
                'user_id'        => session('uid'),
                'con_id'         => 0,
                'con_type'       => 'gezi',
                'title'          => t($_POST['title']),
                'content'        => t($_POST['info']['content']),
                'star_time'      => 0,
                'end_time'       => 0,
                'days'           => 0,
                'article_status' => 0,
                'create_time'    => time(),
            );
            $aid             = M('PayPage')->add($data);
            if ($aid) {
                $aid = array(
                    'aid'   => $aid,
                    'title' => $data['title']
                );
                session('article_id', $aid);
                $this->display();
                exit('<script>alert(\'发布成功返回编辑\');window.close();</script>');
            } else {
                $this->error('发布失败');
            }
            exit;
        }
        $this->assign('allrignt', true);
        $this->display();
    }

    public function articleID() {
        $aid = session('article_id');
        if ($aid) {
            $data = array(
                'status' => true,
                'msg'    => 1,
                'data'   => $aid,
            );
            session('article_id', null);
        } else {
            $data = array(
                'status' => false,
                'msg'    => 1,
                'data'   => 0,
            );
        }
        $this->ajaxReturn($data);
    }

}
